package com.wny.activities;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.wny.backend.Event;
import com.wny.backend.FormattedDisplay;
import com.wny.R;
import com.wny.backend.SocketsClient;

public class EventActivity extends Activity implements LocationListener {
	int uid;
	int eid;
	boolean joined;
	LocationManager myProvider;
	Location myLocation;
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		myProvider = (LocationManager) getSystemService(LOCATION_SERVICE);
		Bundle extras = getIntent().getExtras();
		eid = extras.getInt("eid");
		joined = extras.getBoolean("joined");
		uid = extras.getInt("uid");
		setContentView(R.layout.event);
		myProvider = (LocationManager) getSystemService(LOCATION_SERVICE);
		try {
			displayEventView();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void onResume()
	{
		super.onResume();
		myProvider.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);
	}
	public void displayEventView() throws IOException {
		char [] event_info;
		String event_info_string;
		String event_name;
		String event_description;
		String event_date;
		String event_time;
		String event_longitude;
		String event_latitude;
		ArrayList<FormattedDisplay> attendees;
		SocketsClient myComm = SocketsClient.getSocket();
		event_info=myComm.sendData(("get=event&eid="+eid+"\n").getBytes());
		
		event_info_string=String.valueOf(event_info);
		
		Event this_event=new Event(event_info_string);
		
		event_name=this_event.getname();
		event_description=this_event.getdescription();
		event_date=this_event.getdate();
		event_time=this_event.gettime();
		event_latitude=this_event.getlatitude();
		event_longitude=this_event.getlongitude();
		
		
		TextView eventname=((TextView)findViewById(R.id.eventname));
		TextView eventdate=((TextView)findViewById(R.id.eventdate));
		TextView eventtime=((TextView)findViewById(R.id.eventtime));
		TextView eventdescription=((TextView)findViewById(R.id.eventdescription));
	//	TextView eventlatitude=((TextView)findViewById(R.id.eventlatitude));
	//	TextView eventlongitude=((TextView)findViewById(R.id.eventlongitude));
		
		eventname.setText(event_name);
		eventdate.setText(event_date);
		eventtime.setText(event_time);
		eventdescription.setText(event_description);
		//eventlatitude.setText(event_latitude);
		//eventlongitude.setText(event_longitude);
		
//		attendees=displayAttendees();
//		populateList(attendees);
		/*
		for (int i=0; i<attendees.length; i++){
			displayWithToast(attendees[i],4000);
		}
		
		setListAdapter(new ArrayAdapter<String>(this, R.id.attendeelist, attendees));
		*/
	}
	public void AddEvent(int event_id) throws IOException{
		
		char [] result;
		SocketsClient myComm = SocketsClient.getSocket();
		result = myComm.sendData(("add=eventList&uid="+uid+"&eid="+event_id+"\n").getBytes());
		displayWithToast("Added event to eventlist", 4000);
		
	}
	public void removeEvent(int event_id) throws IOException
	{
		char[] result=null;
		SocketsClient myComm = SocketsClient.getSocket();
		try {
			result = myComm.sendData(("del=eventList&uid="+uid+"&eid="+event_id+"\n").getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (result[0]!='0'){
			displayWithToast("Event deleted.", 4000);
		}
	}
	public boolean onCreateOptionsMenu(Menu menu) {
		// super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		//if (requestType == "myEvents")
		if (joined)
			inflater.inflate(R.menu.joined_event_menu,menu);
		else
			inflater.inflate(R.menu.event_view_menu, menu);
		return true;
	}
	public void displayAttendees()
	{
		Intent displayAttendeesIntent = new Intent(EventActivity.this, DisplayListActivity.class);
		displayAttendeesIntent.putExtra("uid", uid);
		displayAttendeesIntent.putExtra("eid", eid);
		displayAttendeesIntent.putExtra("request", "get=user&eventList.eid="+eid+ "&uid="+uid+ "\n");
		displayAttendeesIntent.putExtra("requestType", "user");
		startActivity(displayAttendeesIntent);
	}
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.Join_event:
			try {
				AddEvent(eid);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return true;
		case R.id.remove_event:
			try {
				removeEvent(eid);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return true;
		case R.id.view_attendees:
			displayAttendees();
			return true;
		case R.id.view_attendees_o:
			displayAttendees();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	// a wrapper function for the Toast class. Used to show messages onscreen
	private void displayWithToast(String toShow, int duration) {
		Toast temp = Toast.makeText(this, toShow, duration);
		temp.setGravity(Gravity.CENTER, 0, 0);
		temp.show();
	}
	public void onLocationChanged(Location location)
	{
		// TODO Auto-generated method stub
		myLocation = location;
		SocketsClient myComm = SocketsClient.getSocket();
		try {
			myComm.sendNonblocking(("loc&"+location.getLatitude()+"&"+location.getLongitude()+"&"+this.uid+"\n").getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	//notifies user GPS disables
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		displayWithToast("The system GPS service has been disabled.", 5000);
	}

	//notifies user GPS enabled
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
		displayWithToast("GPS is now avaliable", 4000);

	}

	//notifies user of any change in the status of the GPS
	public void onStatusChanged(String location_provider, int status, Bundle otherVariables) {
		// TODO Auto-generated method stub
		switch (status)
		{
		case LocationProvider.OUT_OF_SERVICE:
			displayWithToast("The GPS is out of service.", 5000);
		case LocationProvider.TEMPORARILY_UNAVAILABLE:
			displayWithToast("The GPS is temporarially unavaliable", 5000);
		case LocationProvider.AVAILABLE:
			displayWithToast("GPS is now avaliable", 5000);
		}

	}

}
